<?php
/*
Plugin Name: Sonim product
Plugin URI: 
Description: Sonim product manager
Version: 1.0
Author: HiepTq
Author URI: http://hiepvotinh.com
/* ----------------------------------------------*/
if ( ! defined( 'WP_CONTENT_DIR' ) )
    define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );
if ( ! defined( 'WP_PLUGIN_DIR' ) )
    define( 'WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins' );
if ( ! defined( 'WP_CONTENT_URL' ) )
    define( 'WP_CONTENT_URL', get_option( 'siteurl' ) . '/wp-content' );
if ( ! defined( 'WP_PLUGIN_URL' ) )
    define( 'WP_PLUGIN_URL', WP_CONTENT_URL. '/plugins' );
add_action('init', 'sonim_product_register');
 
function sonim_product_register() {
 
	$labels = array(
		'name' => _x('Sonim product', 'post type general name'),
		'singular_name' => _x('Sonim product Item', 'post type singular name'),
		'add_new' => _x('Add New', 'sonim_product item'),
		'add_new_item' => __('Add New Sonim product Item'),
		'edit_item' => __('Edit Sonim product Item'),
		'new_item' => __('New Sonim product Item'),
		'view_item' => __('View Sonim product Item'),
		'search_items' => __('Search Sonim product'),
		'not_found' =>  __('Nothing found'),
		'not_found_in_trash' => __('Nothing found in Trash'),
		'parent_item_colon' => ''
	);
 
	$args = array(
		'labels' => $labels,
		'public' => true,
		'publicly_queryable' => true,
		'show_ui' => true,
		'query_var' => true,
		'menu_icon' => get_stylesheet_directory_uri() . '/article16.png',
		'rewrite' => true,
		'capability_type' => 'post',
		'hierarchical' => false,
		'menu_position' => null,
		'supports' => array('title','editor','thumbnail')
	  ); 
	register_post_type( 'sonim_product' , $args );
}

/*register_taxonomy("sonim_categories", array("sonim_product"), array("hierarchical" => true, "label" => "Product Categories", "singular_label" => "Product Category","capabilities" => array('manage_terms'), "rewrite" => array( 'slug' => 'product' )));*/


define('TEMPLATEPATH', get_template_directory());
// lets use the WPAlchemy helper
include_once WP_CONTENT_DIR . '/wpalchemy/MetaBox.php';
 
// custom constant (opposite of TEMPLATEPATH)
define('_TEMPLATEURL',WP_CONTENT_URL . '/themes/' . basename(TEMPLATEPATH));
 
// custom css for our meta boxes
if (is_admin()) wp_enqueue_style('custom_meta_css',_TEMPLATEURL . '/custom/meta.css');
 
// create the meta box
$sonim_metabox_description = new WPAlchemy_MetaBox(array
(
	'id' => '_sonim_metabox_description',
	'title' => 'Description',
	'template' => WP_PLUGIN_DIR.'/sonim-product/sonim_metabox_description.php',
	'types' => array('sonim_product')
));

$sonim_metabox_tech_info = new WPAlchemy_MetaBox(array
(
	'id' => '_sonim_metabox_tech_info',
	'title' => 'Tech Info',
	'template' => WP_PLUGIN_DIR.'/sonim-product/sonim_metabox_tech_info.php',
	'types' => array('sonim_product')
));

$sonim_metabox_links = new WPAlchemy_MetaBox(array
(
	'id' => '_sonim_metabox_links',
	'title' => 'Links',
	'template' => WP_PLUGIN_DIR.'/sonim-product/sonim_metabox_links.php',
	'types' => array('sonim_product')
));

$sonim_metabox_extra_css_class = new WPAlchemy_MetaBox(array
(
	'id' => '_sonim_metabox_extra_css_class',
	'title' => 'Extra css',
	'template' => WP_PLUGIN_DIR.'/sonim-product/sonim_metabox_extra_css_class.php',
	'types' => array('sonim_product','post','page')
));
 
// important: note the priority of 99, the js needs to be placed after tinymce loads
add_action('admin_print_footer_scripts','my_admin_print_footer_scripts',99);
function my_admin_print_footer_scripts()
{
	?><script type="text/javascript">/* <![CDATA[ */
		jQuery(function($)
		{
			var i=1;
			$('.customEditor textarea').each(function(e)
			{
				var id = $(this).attr('id');
 
				if (!id)
				{
					id = 'customEditor-' + i++;
					$(this).attr('id',id);
				}
 
				tinyMCE.execCommand('mceAddControl', false, id);
 
			});
		});
	/* ]]> */</script><?php
} 
 
add_action("manage_posts_custom_column",  "sonim_product_custom_columns");
add_filter("manage_edit-sonim_product_columns", "sonim_product_edit_columns");
 
function sonim_product_edit_columns($columns){
  $columns = array(
    "cb" => "<input type=\"checkbox\" />",
	"image" => "Image",
    "title" => "Product name",
    //"description" => "Description",
   // "categories" => "Categories",
  );
 
  return $columns;
}

function sonim_product_custom_columns($column){
  global $post;
 
  switch ($column) {
    case "image":
      echo get_the_post_thumbnail( $post->ID, 'thumb');
      break;
  }
}

function sonim_product_list_slideshow_func($atts) {
     extract(shortcode_atts(array(
	      'num' => '4'
     ), $atts));
	 $products = query_posts( array('post_type'=>'sonim_product','posts_per_page'=>'-1'));
	 $num = min((int)$num,count($products));
	 $html = array();
	 $display_control = '';
	if($num == count($products))$display_control = 'style="display:none"';
	 $html[]='<div class="slideshow-container" >';
		$html[]='<a '.$display_control.' href="#" class="prev carousel-control" id="sonim-product-slideshow-prev">&nbsp</a>';
		$html[]='<div id="sonim-product-slideshow" class="sonim-product-slideshow">';
			$html[]='<ul>';
				foreach($products as $product){
					$html[] = '<li><a href="'.get_permalink( $product->ID ).'">'.get_the_post_thumbnail( $product->ID, 'full').'</a></li>';
				}
			$html[]='</ul>';
		$html[]='</div>';
		$html[]='<a '.$display_control.' href="#" class="next carousel-control" id="sonim-product-slideshow-next">&nbsp</a>';
	 $html[]='</div>';
	 $html[]='<script>
				jQuery(document).ready(function(){
					jQuery("#sonim-product-slideshow").jCarouselLite({
						btnNext: "#sonim-product-slideshow-next",
						btnPrev: "#sonim-product-slideshow-prev",
						visible:'.$num.',
						circular:false
					});
				})
			</script>';
	 
     return implode("\n",$html);
}
add_shortcode('sonim_products_slideshow', 'sonim_product_list_slideshow_func');

function sonim_product_theme_scripts()
{
   wp_enqueue_script('sonim_product_tabify', WP_PLUGIN_URL . '/sonim-product/js/jquery.tabify.js',array('jquery'),'1.0',true);
   wp_enqueue_script('sonim_product_carousel', WP_PLUGIN_URL . '/sonim-product/js/jcarousellite_1.0.1.min.js',array('jquery'),'1.0',true);
}
add_action('wp_enqueue_scripts','sonim_product_theme_scripts');